<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  
  <script>
    // var performanceS = function(salary) {
    //   return salary * 4;
    // }
    // var performanceA = function(salary) {
    //   return salary * 3;
    // }
    // var performanceB = function(salary) {
    //   return salary * 2;
    // }
    // var calculateBouns = function(performanceLevel,salary) {
    //   if(performanceLevel == 'S') {
    //     return performanceS(salary);
    //   } else if(performanceLevel == 'A') {
    //     return performanceA(salary);
    //   } else if(performanceLevel == 'B') {
    //     return performanceB(salary);
    //   }
    // }

    // console.log(calculateBouns('A',4000))

    //---------------------------------------------------------------------------------------------------------------
    // var performanceS = function(){};
    // performanceS.prototype.calculate = function(salary) {
    //   return salary * 4;
    // }

    // var performanceA = function(){};
    // performanceA.prototype.calculate = function(salary) {
    //   return salary * 3;
    // }

    // var performanceB = function(){};
    // performanceB.prototype.calculate = function(salary) {
    //   return salary * 2;
    // }
    
    // var Bonus = function() {
    //   this.salary = null;  //原始工资
    //   this.strategy = null; //绩效登记对应的策略对象
    // }

    // Bonus.prototype.setSalary = function(salary) {
    //   this.salary = salary;
    // }

    // Bonus.prototype.setStrategy = function(strategy) {
    //   this.strategy = strategy;
    // }

    // Bonus.prototype.getBouns = function() {
    //   if(!this.strategy) {
    //     throw new Error('未设置strategy属性')
    //   }
    //   if(!this.salary) {
    //     throw new Error('未设置salary属性')
    //   }
    //   return this.strategy.calculate(this.salary);//把计算奖金的操作委托给对应的策略对象
    // }

    // var bonus = new Bonus();
    // bonus.setSalary(1000);
    // bonus.setStrategy( new performanceA())
    // console.log(bonus.getBouns())

    //---------------------------------------------------------------------------------------------------------------

    var strategies = {
      "S": function(salary) {
        return salary * 4;
      },
      "A": function(salary) {
        return salary * 3;
      },
      "B": function(salary) {
        return salary * 2;
      },
      "C": function(salary) {
        return salary * 1.5;
      }
    }

    var calculateBonus = function(level,salary) {
      return strategies[level](salary);
    }
    console.log(calculateBonus('S',200))
    console.log(calculateBonus('C',1000))


    
  </script>
</body>
</html>